package com.p2p.cms.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.p2p.base.annotation.LoginNeedless;
import com.p2p.base.cache.Cache;
import com.p2p.base.page.PageVo;
import com.p2p.base.web.controller.BaseController;
import com.p2p.cms.service.CategoryService;
import com.p2p.cms.service.PostService;

/**
 * 
 * @author fansunion@qq.com 2014年11月2日
 */
@Controller
@RequestMapping("post")
@LoginNeedless
public class PostController extends BaseController {
	private static final String CATEGORY_ROWS = "categoryRows";

	@Autowired
	private PostService postService;

	@Autowired
	private CategoryService categoryService;
	
	private String sysConfigKey = "global_sysConfigMapAll_";
	
	@Resource
	private Cache defaultCache;


	// 获得一页内容
	@RequestMapping(value = "list")
	public String list(PageVo page, Model model) {
		PageVo vo = postService.page(page);
		model.addAttribute("page", vo);
		prepareCategoryRows(vo);
		return "post/index";
	}
	
	@RequestMapping("/postView/{postId}")
	public String postView(@PathVariable("postId") Integer postId,Model model){
		Map<String, Object> postObject = postService.get(postId);
		if(postObject!=null)
		model.addAttribute("post", postObject);
		return "post/postView";
	}
	@RequestMapping(value = "listTitleByCategoryId")
	public String list(Integer categoryId,Integer postId,Model model) {
		if(categoryId==null){
			String defaultCategory = getDefaultCategoryId();
			if(defaultCategory!=null){
				String[] array = defaultCategory.split(":");
				categoryId= Integer.valueOf(array[0]);
				postId = Integer.valueOf(array[1]);
			}
		}
		if(categoryId==null){
			return "post/listTitle";
		}
		Map<String,Object> category = categoryService.get(categoryId);
		if(category==null){
			return "post/listTitle";
		}
		model.addAttribute("categoryId",categoryId);
		model.addAttribute("postId",postId);
		
		model.addAttribute("categoryname",category.get("name"));
		List<Map<String, Object>> postlist = postService.listTitleByCategoryId(categoryId, 10);
		model.addAttribute("postTitleList", postlist);
		if(postId!=null){
			Map<String, Object> postObject = postService.get(postId);
			if(postObject!=null)
			model.addAttribute("postView", postObject);
		}

		return "post/listTitle";
	}
	
	private String getDefaultCategoryId(){
		Object o = defaultCache.getValue(sysConfigKey);
		if(o!=null){
			Map<String,String> sysConfigMap = (Map<String,String>)o;
			String value = sysConfigMap.get("CmsCategoryIdConfig");
			if(value!=null){
				return value;
			}
		}
		return null;
	}
	
	
	//对外提供的服务接口
	@RequestMapping(value = "recentpost")
	public void recentPost(Integer categoryid,String jsoncallback,Integer count,Model model,HttpServletResponse response) {
		List<Map<String,Object>> list = postService.listRecent(categoryid,count);
		//JSONObject json = new JSONObject();
		//json.put("list", list);
		//String str=json.toJSONString();
		//model.addAttribute("callback", list);
		 String str=JSONObject.toJSONString(list);
		 str =jsoncallback+"("+str+")";
		super.returnMessage(response, str);
	}

	private void prepareCategoryRows(PageVo vo) {
		List<Map<String, Object>> categoryRows = categoryService.listAll();
		vo.getOtherData().put(CATEGORY_ROWS, categoryRows);
	}

	// 查看post，id为post的id
	@RequestMapping(value = "/detail/{id}")
	public String view(@PathVariable("id") Integer id, Model model) {
		Map<String, Object> post = postService.get(id);
		model.addAttribute("post", post);
		prepareCategoryRowsForView(model, post);
		return "post/view";
	}

	private void prepareCategoryRowsForView(Model model,
			Map<String, Object> params) {
		List<Map<String, Object>> categoryRows = categoryService.listAll();
		model.addAttribute(CATEGORY_ROWS, categoryRows);
	}
	
	
	@RequestMapping(value = "/listTitleByCategoryId2")
	public String listTitleByCategoryId2(Integer categoryId,Model model){
		Map<String,Object> category =categoryService.get(categoryId);
		if(category!=null&&category.get("status").equals(0)){
			List<Map<String, Object>> postlist = this.listAllInfoByCategoryId(categoryId);
			category.put("child", postlist);
			model.addAttribute("categoryTitle", category);
		}
		
		
		return "post/view2";
	}
	
	private List<Map<String,Object>> listAllInfoByCategoryId(Integer categoryId){
		List<Map<String, Object>> postlist = postService.listTitleByCategoryId(categoryId, 10);
		//List<Integer> childCategoryIdList = categoryService.getChildIdList(categoryId);
		List<Map<String,Object>> childCategoryList = categoryService.getChildSimpleList(categoryId);
		//postlist.addAll(childCategoryList);
		for(Map<String,Object> child :childCategoryList){
			postlist.add(child);
			List<Map<String, Object>> childPostInfo = this.listAllInfoByCategoryId((Integer)child.get("id"));
			if(childPostInfo.size()>0){
				child.put("child", childPostInfo);
			}
		}
		return postlist;
	}
}
